package com.lekang.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.domain.DropContent;
import com.lekang.service.IDropContentService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 退住内容记录Controller
 * 
 * @author chengbingzhen
 * @date 2025-10-18
 */
@RestController
@RequestMapping("/drop/content")
public class DropContentController extends BaseController
{
    @Autowired
    private IDropContentService dropContentService;

    /**
     * 查询退住内容记录列表
     */
    @PreAuthorize("@ss.hasPermi('drop:content:list')")
    @GetMapping("/list")
    public TableDataInfo list(DropContent dropContent)
    {
        startPage();
        List<DropContent> list = dropContentService.selectDropContentList(dropContent);
        return getDataTable(list);
    }

    /**
     * 导出退住内容记录列表
     */
    @PreAuthorize("@ss.hasPermi('drop:content:export')")
    @Log(title = "退住内容记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DropContent dropContent)
    {
        List<DropContent> list = dropContentService.selectDropContentList(dropContent);
        ExcelUtil<DropContent> util = new ExcelUtil<DropContent>(DropContent.class);
        util.exportExcel(response, list, "退住内容记录数据");
    }

    /**
     * 获取退住内容记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('drop:content:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dropContentService.selectDropContentById(id));
    }

    /**
     * 新增退住内容记录
     */
    @PreAuthorize("@ss.hasPermi('drop:content:add')")
    @Log(title = "退住内容记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DropContent dropContent) {
        // 1. 调用Service插入数据（插入后dropContent的id会被自动回写，前提是表的主键是自增且实体类正确映射）
        int rows = dropContentService.insertDropContent(dropContent);

        // 2. 判断插入是否成功，成功则返回主键id
        if (rows > 0) {
            // 关键：通过 dropContent.getId() 获取回写的主键，封装到AjaxResult的data中
            return AjaxResult.success("新增退住内容记录成功").put("dropContentId", dropContent.getId());
        }

        // 3. 插入失败返回错误信息
        return toAjax(rows);
    }

    /**
     * 修改退住内容记录
     */
    @PreAuthorize("@ss.hasPermi('drop:content:edit')")
    @Log(title = "退住内容记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DropContent dropContent)
    {
        return toAjax(dropContentService.updateDropContent(dropContent));
    }

    /**
     * 删除退住内容记录
     */
    @PreAuthorize("@ss.hasPermi('drop:content:remove')")
    @Log(title = "退住内容记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dropContentService.deleteDropContentByIds(ids));
    }
}
